# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=pdm-pep517
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=1.1.4
pkgrel=2
pkgdesc="A PEP 517 backend for PDM that supports PEP 621 metadata (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://github.com/pdm-project/pdm-pep517'
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-cerberus"
         "${MINGW_PACKAGE_PREFIX}-python-license-expression"
         "${MINGW_PACKAGE_PREFIX}-python-packaging"
         "${MINGW_PACKAGE_PREFIX}-python-tomli"
         "${MINGW_PACKAGE_PREFIX}-python-tomli-w")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-pytest")
source=("https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname}-${pkgver}.tar.gz"
        "python-pdm-pep517-1.0.3-devendor.patch")
sha256sums=('7f49121e70b42dca296fac962210dd2da07a39575fc5673137ad661633b2cf3f'
            'd9cd6704b7313eb36edc5a919d6ee8761570d85fb3f795fbfa4eaf34762258c4')
noextract=("${_realname}-${pkgver}.tar.gz")

prepare() {
  tar -xzf "${srcdir}"/${_realname}-${pkgver}.tar.gz || true

  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/python-pdm-pep517-1.0.3-devendor.patch
  rm -frv pdm/pep517/_vendor

  # Remove *.orig files
  find . -type f -name "*.orig" -exec rm -f {} \;
}

build() {
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"

  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

check() {
  cd "${srcdir}/python-build-${MSYSTEM}"

  ${MINGW_PREFIX}/bin/python -m pytest
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}
